package com.ware.jfinal.plugin.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;

import com.ware.jfinal.plugin.shiro.ShiroRealm.ShiroUser;

public abstract class SH {
	public static boolean isAuth(){
		return SecurityUtils.getSubject().isAuthenticated();
	}
	
	public static Subject getSubject(){
		return SecurityUtils.getSubject();
	}
	
	public static ShiroUser getUser(){
		return (ShiroUser)getSubject().getPrincipal();
	}
	public static String login(String userName,String password,boolean remember){
		String result = "success";
		UsernamePasswordToken token = new UsernamePasswordToken();
		token.setUsername(userName);
		token.setPassword(password.toCharArray());
		token.setRememberMe(remember);		
		try {
			SecurityUtils.getSubject().login(token);
		} catch (Exception e) {
			//e.printStackTrace();
			result = "failure";
		} 
		return result;
	}
	
	public static void logout(){
		SecurityUtils.getSubject().logout();
	}
}
